//
//  ItemDetailView.m
//  Menu
//
//  Created by trimad on 11-11-21.
//  Copyright 2011 Trimad Group. All rights reserved.
//

#import "ItemDetailView.h"
#import "Item.h"
#import "MenuViewController.h"
#import "ContextManager.h"
#import "DisplayManager.h"

@interface ItemDetailView(Private)
-(void) handleTapGestures:(UIGestureRecognizer *) sender;
@end


@implementation ItemDetailView

@synthesize imageView;
@synthesize nameLabel;
@synthesize priceLabel;
@synthesize item;

static int IMAGE_HEIGHT = 780;
static int IMAGE_WIDTH = 720;
static int IMAGE_CENTER_X = 384;
static int IMAGE_CENTER_Y = 410;

- (void) activate: (Item *) aItem {
	self.item = aItem;
	nameLabel.text = [[NSString alloc] initWithFormat:@"【%@】", aItem.name];
	priceLabel.text = [[NSString alloc] initWithFormat:@"%.2f (元)", aItem.price];
	if (aItem.largeImageName != nil && ![aItem.largeImageName isEqualToString:@""]) {
		imageView.image = [UIImage imageNamed:aItem.largeImageName];
	} else {
		imageView.image = [UIImage imageNamed:aItem.midImageName];
	}
	CGSize scaledSize;
	float scalePortion;
	
	if (imageView.image.size.height > imageView.image.size.width) {
		scalePortion = (float)IMAGE_HEIGHT/imageView.image.size.height;
		scaledSize.width = (int)imageView.image.size.width * scalePortion;
		scaledSize.height = IMAGE_HEIGHT;
	} else {
		scalePortion = (float)IMAGE_WIDTH/imageView.image.size.width;
		scaledSize.height = (int)imageView.image.size.height * scalePortion;
		scaledSize.width = IMAGE_WIDTH;
	}
	
	CGRect frame = imageView.frame;
	frame.size = scaledSize;
	imageView.frame = frame;
	
	CGPoint centerPoint = imageView.center;
	centerPoint.x = IMAGE_CENTER_X;
	centerPoint.y = IMAGE_CENTER_Y;
	imageView.center = centerPoint;
	
	UITapGestureRecognizer *tapToOpenGesture = 
	[[UITapGestureRecognizer alloc] initWithTarget:self 
											action:@selector(handleTapGestures:)];
	tapToOpenGesture.numberOfTapsRequired = 1;
	[self addGestureRecognizer:tapToOpenGesture];
}

-(void) handleTapGestures:(UIGestureRecognizer *) sender {
	[[[self superview] superview] removeFromSuperview];
	int pageIndex = [[DisplayManager instance] pageIndex:item.code];
	MenuViewController *menuVC=[[ContextManager getInstance] rootViewController];
	[menuVC jumpToPage:pageIndex];
	
	menuVC.view.alpha = 0.7f;
	[UIView animateWithDuration:0.5f delay:0 options:UIViewAnimationOptionCurveLinear 
					 animations:^{
						 menuVC.view.alpha = 1.0f;
					 }
					 completion:nil
	 ];
}

-(void) dealloc {
	[imageView release];
	[nameLabel release];
	[priceLabel release];
	[item release];
	[super dealloc];
}
@end
